<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

class setNavigation extends Controller
{
	public function __construct()
	{
		parent::__construct();		
	}
	
	public function run()
	{
		$str  = "<div id=\"accordion\">\n";
		$str .= $this->getCategory();
		$str .= $this->getBrand();
		$str .= $this->getTags();
		$str .= "</div>\n";
		
		echo $str;
	}
	
	public function getCategory()
	{
		$nav = array();
		$this->import('Database');
		$objRd = $this->Database->execute("SELECT tl_taxonomy.id AS id, pid, alias, name FROM tl_taxonomy WHERE `published`=1 Order by sorting asc");
		while ($objRd->next())
		{
			$nav[$objRd->pid][] = array('id'=>$objRd->id, 'name'=>$objRd->name);
		}
		
		$str  = "<div class='bycategory'>\n";
		$str .= "<dl>\n";
		$str .= "<dt class=\"select first\"><a href=\"#\">BY CATEGORY</a></dt>\n";
		$str .= "<dd style=\"display:block;\">\n";
		$str .= "<ul class=\"level_1\">\n";
		
		$categoryId  = (isset($_GET['category']) && $_GET['category'] != "") ? $_GET['category'] : "";
		
		for($i=0;$i<count($nav[0]);$i++){
			$id   = $nav[0][$i]['id'];
			$name = $nav[0][$i]['name'];
	
			if($categoryId==$id){
				$class = ($i==0) ? "class='selected first'" : "class='selected'";
			}else{
				$class = ($i==0) ? "class='first'" : "";
			}
	
			$str .= "<li $class>\n";
			//$str .= "<a href=\"./?category=$id\" title=\"$name\">$name</a>\n";
			$str .= "$name\n";
			
			if($nav[$id]!=Null){
				$str .= "<ul class=\"level_2\">\n";
				for($j=0;$j<count($nav[$id]);$j++){
					$sid  	= $nav[$id][$j]['id'];
					$sname  = $nav[$id][$j]['name'];
					
					if($categoryId==$sid){
						$class = ($j==0) ? "class='selected first'" : "class='selected'";
					}else{
						$class = ($j==0) ? "class='first'" : "";
					}					

					$str   .= "<li $class><a href=\"./?type=all&category=$sid\" title=\"$sname\">$sname</a></li>\n";
				}
				$str .= "</ul>\n";
			}
			
			$str .= "</li>\n";
		}
		
		$str .= "</ul></dd></dl></div>\n";
		
		return $str;
	}
	
	public function getBrand()
	{
		$this->import('Database');
		
		$objRd = $this->Database->execute("SELECT catalog_brand.id AS id, brand FROM catalog_brand Order by sorting asc");
		
		$str  = "<div class='bybrand'>\n";
		$str .= "<dl>\n";
		$str .= "<dt class=\"second select\"><a href=\"#\">BY BRAND</a></dt>\n";
		$str .= "<dd style=\"display:block;\">\n";
		$str .= "<ul class=\"level_1\">\n";
		
		$brandId  = (isset($_GET['brand']) && $_GET['brand'] != "") ? $_GET['brand'] : "";
		
		$i = 0;
		while ($objRd->next())
		{
			if($brandId==$objRd->id){
				$class = ($i==0) ? "class='selected first'" : "class='selected'";
			}else{
				$class = ($i==0) ? "class='first'" : "";
			}
			
			$str   .= "<li $class><a href=\"./?brand=$objRd->id&type=all\" title=\"$objRd->brand\">$objRd->brand</a></li>\n";
			$i++;
		}
		
		$str .= "</ul></dd></dl></div>\n";
		
		return $str;
	}	
	
	public function getTags()
	{
		$this->import('Database');
		
		$objRd = $this->Database->execute("SELECT catalog_tags.id AS id, tag FROM catalog_tags Order by sorting asc");
		
		$str  = "<div class='bytags'>\n";
		$str .= "<dl>\n";
		$str .= "<dt class=\"third select\"><a href=\"#\">POPULAR SEARCHES</a></dt>\n";
		$str .= "<dd style=\"display:block;\">\n";
		$str .= "<ul class=\"level_1\">\n";
		
		$brandId  = (isset($_GET['tag']) && $_GET['tag'] != "") ? $_GET['tag'] : "";
		
		$i = 0;
		while ($objRd->next())
		{
			if($brandId==$objRd->id){
				$class = ($i==0) ? "class='selected first'" : "class='selected'";
			}else{
				$class = ($i==0) ? "class='first'" : "";
			}
			
			$str   .= "<li $class><a href=\"./?tag=$objRd->id&type=all\" title=\"$objRd->tag\">$objRd->tag</a></li>\n";
			$i++;
		}
		
		$str .= "</ul></dd></dl></div>\n";
		
		return $str;
	}	
	
}


$objGetRd = new setNavigation();
$objGetRd->run();

?>